home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / viewers / prev / prev.lha / object.c < prev    next >
C/C++ Source or Header  |  1991-03-11  |  5KB  |  269 lines

  1. #include <stdio.h>
  2. #include "art.h"
  3. #include "gram.h"
  4. #include "objs.h"
  5. #include "macro.h"
  6.  
  7. extern attr    astack[], *astackp;
  8. extern mats    mstack[], *mstackp;
  9.  
  10. extern matrix    trans;
  11.  
  12. extern float    eval_fexpr();
  13. extern object    *compinit(), *csginit();
  14.  
  15. extern symbol    **ostackp, *ostack[];
  16.  
  17. /*
  18.  * defobj
  19.  *
  20.  *    define an object
  21.  */
  22. defobj(s, type, d)
  23.     char    *s;
  24.     int    type;
  25.     details    *d;
  26. {
  27.     symbol    *sym;
  28.  
  29.     sym = insertsym(ostackp, s);
  30.  
  31.     sym->type = type;
  32.     sym->u.det = d;
  33. }
  34.  
  35. /*
  36.  * lookup
  37.  *
  38.  *    check to see if string represents a predefined object type
  39.  */
  40. symbol *
  41. lookup(string)
  42.     char    *string;
  43. {
  44.     symbol    *sym, **symptr;
  45.  
  46.     symptr = ostackp;
  47.  
  48.     while ((sym = findsym(*symptr, string)) == (symbol *)NULL)
  49.         if (symptr-- == ostack)
  50.             return((symbol *)NULL);
  51.  
  52.     return(sym);
  53. }
  54.  
  55. /*
  56.  * objectinit
  57.  *
  58.  *    return an initialised object 
  59.  */
  60. object *
  61. objectinit(sym, d)
  62.     symbol    *sym;
  63.     details    *d;
  64. {
  65.     int    type;
  66.     object    *o;
  67.     details    *nd1, *nd2, *nd3;
  68.     details    *nd, *dl, *nxtd, *otherdt, *argdt, *nxtdl;
  69.     surface    s;
  70.     int    sset;
  71.     char    buf[BUFSIZ];
  72.  
  73.                     /* might have a composite */
  74.     if (sym == (symbol *)NULL || sym->type == COMP_OBJ)
  75.         return(compinit(sym, d));
  76.  
  77.     if (sym->type == CSG_OBJ)
  78.         return(csginit(sym, d));
  79.  
  80.     astackp++;
  81.     *astackp = *(astackp - 1);
  82.     mstackp++;
  83.     *mstackp = *(mstackp - 1);
  84.     mident4(mstackp->vm);
  85.  
  86.     astackp->txtlist = (texture *)NULL;
  87.  
  88.     s = *astackp->s;
  89.  
  90.     o = (object *)smalloc(sizeof(object));
  91.  
  92.     o->type = type = sym->type;
  93.     o->incsg = FALSE;
  94.  
  95.     o->nxt = (object *)NULL;
  96.  
  97.                     /* reverse list */
  98.     argdt = (details *)NULL;
  99.     for (dl = d; dl != (details *)NULL; dl = nxtdl) {
  100.         nxtdl = dl->nxt;
  101.         dl->nxt = argdt;
  102.         argdt = dl;
  103.     }
  104.  
  105.     otherdt = (details *)NULL;
  106.  
  107.     if (sym->u.det != (details *)NULL) {
  108.         for (dl = sym->u.det; dl != (details *)NULL; dl = dl->nxt) {
  109.             nd = (details *)smalloc(sizeof(details));
  110.             *nd = *dl;
  111.             nd->nxt = otherdt;
  112.             otherdt = nd;
  113.             if (nd->type == COMPLEXVERTEX) {
  114.                 nd1 = (details *)smalloc(sizeof(details));
  115.                 *nd1 = *nd->u.det;
  116.                 if (nd1->nxt != (details *)NULL) {
  117.                     nd1->nxt = nd2 = (details *)smalloc(sizeof(details));
  118.                     *nd2 = *nd->u.det;
  119.                     if (nd2->nxt != (details *)NULL) {
  120.                         nd2->nxt = nd3 = (details *)smalloc(sizeof(details));
  121.                         *nd3 = *nd->u.det;
  122.                     }
  123.                 }
  124.                 nd->u.det = nd1;
  125.             }
  126.         }
  127.     }
  128.  
  129.     if (argdt == (details *)NULL) {
  130.         argdt = otherdt;
  131.         otherdt = (details *)NULL;
  132.     }
  133.  
  134.     nd = (details *)NULL;
  135.  
  136.     sset = FALSE;
  137.  
  138.     for (dl = argdt; dl != (details *)NULL; dl = nxtd) {
  139.  
  140.         if ((nxtd = dl->nxt) == (details *)NULL) {
  141.             nxtd = otherdt;
  142.             otherdt = (details *)NULL;
  143.         }
  144.  
  145.         switch (dl->type) {
  146.         case COLOUR:
  147.             s.c.r = dl->u.c.r;
  148.             s.c.g = dl->u.c.g;
  149.             s.c.b = dl->u.c.b;
  150.             sset = TRUE;
  151.             free(dl);
  152.             break;
  153.         case AMBIENT:
  154.             s.a.r = dl->u.c.r;
  155.             s.a.g = dl->u.c.g;
  156.             s.a.b = dl->u.c.b;
  157.             sset = TRUE;
  158.             free(dl);
  159.             break;
  160.         case TEXTURE:
  161.             free(dl);
  162.             break;
  163.         case MATERIAL:
  164.             s.ri = dl->u.mat.ri;
  165.             s.kd = dl->u.mat.kd;
  166.             s.ks = dl->u.mat.ks;
  167.             s.ksexp = dl->u.mat.ksexp;
  168.             sset = TRUE;
  169.             free(dl);
  170.             break;
  171.         case REFLECTANCE:
  172.             s.refl = dl->u.f;
  173.             sset = TRUE;
  174.             free(dl);
  175.             break;
  176.         case TRANSPARENCY:
  177.             s.trans = dl->u.f;
  178.             sset = TRUE;
  179.             free(dl);
  180.             break;
  181.         case ABSORPTION:
  182.             s.falloff = dl->u.f;
  183.             sset = TRUE;
  184.             free(dl);
  185.             break;
  186.         case ART_TRANSLATE:
  187.             art_translate(dl->u.v.x, dl->u.v.y, dl->u.v.z);
  188.             free(dl);
  189.             break;
  190.         case ART_SCALE:
  191.             art_scale(dl->u.v.x, dl->u.v.y, dl->u.v.z);
  192.             free(dl);
  193.             break;
  194.         case ART_ROTATE:
  195.             art_rotate(dl->u.rot.ang, dl->u.rot.axis);
  196.             free(dl);
  197.             break;
  198.         case ON:
  199.             astackp->options |= dl->u.i;
  200.             free(dl);
  201.             break;
  202.         case OFF:
  203.             astackp->options &= ~dl->u.i;
  204.             free(dl);
  205.             break;
  206.         default:
  207.             dl->nxt = nd;
  208.             nd = dl;
  209.         }
  210.     }
  211.  
  212.     if (sset) {
  213.         astackp->s = (surface *)smalloc(sizeof(surface));
  214.         *astackp->s = s;
  215.         astackp->slevel = astackp - astack;
  216.     }
  217.  
  218.     if (astackp->s->trans != 0.0 && astackp->s->ri == 0.0)
  219.         astackp->s->ri = 1.0;
  220.  
  221.     switch (type) {
  222.     case SPHERE:
  223.     case ELLIPSOID:
  224.         sphereinit(o, nd);
  225.         break;
  226.     case BOX:
  227.         boxinit(o, nd);
  228.         break;
  229.     case CYLINDER:
  230.         cylinit(o, nd);
  231.         break;
  232.     case POLYGON:
  233.         polygoninit(o, nd);
  234.         break;
  235.     case CONE:
  236.         coneinit(o, nd);
  237.         break;
  238.     case RING:
  239.         ringinit(o, nd);
  240.         break;
  241.     case TORUS:
  242.         torusinit(o, nd);
  243.         break;
  244.     case SUPERQUADRIC:
  245.         superinit(o, nd);
  246.         break;
  247.     case GEOMETRY:
  248.         geometryinit(o, nd);
  249.         break;
  250.     /*
  251.  
  252.     case ALGEBRAIC:
  253.         alginit(o, nd);
  254.         break;
  255.         */
  256.     default:
  257.         /*
  258.         sprintf(buf, "art: unkown object type %d in objectinit.\n", type);
  259.         fatal(buf);
  260.     */
  261.         ;
  262.     }
  263.  
  264.     astackp--;
  265.     mstackp--;
  266.  
  267.     return(o);
  268. }
  269.